package demo.Thread;


import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/8/11-08-11-17:21
 * @Description：demo.Thread      //多线程文件下载
 */
public class Download_thread_Stream {
    //步骤
    //获取目标文件的大小
    //在本地创建一个空壳并且指定大小
    //创建线程分别到指定位置下载文件并且将下载的文件内容要插入到本地空壳文件的指定位置
    public static void main(String[] args) {
        //String address = "https://up.tt98.com/uploadfile/sjbz/2020-12-02/e53a469a9a94bd3ff5415b2481fd6eaf.jpg";
        String address = "https://n.sinaimg.cn/sinakd10116/742/w640h902/20200804/b998-ixeeisa1665497.jpg";
        String myaddress = "C:\\Users\\hjy\\Pictures\\jjy3.jpg";

        int contentLength = GetFileLength(address,myaddress);

        int countThread = 6;
        int per = contentLength/countThread;
        for (int i = 0; i < countThread; i++) {
            if(i == countThread-1){ //最后一个线程
                new DownloadTread(address,myaddress,i*per,contentLength-1).start();
            }else {
                new DownloadTread(address,myaddress,i*per,(i+1)*per-1).start();
            }
        }
    }

    private static int GetFileLength(String address, String myaddress) {
        RandomAccessFile file = null;
        HttpURLConnection conn = null;
        try{
            URL url = new URL(address);
            conn = (HttpURLConnection) url.openConnection();

            int contantLength = conn.getContentLength();
            //在目标文件创建文件夹
            file = new RandomAccessFile(myaddress,"rw");
            file.setLength(contantLength);

            return contantLength;
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if(conn != null){
                conn.disconnect();
            }
            if(file != null){
                try {
                    file.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return 0;
    }


}
class DownloadTread extends Thread{
    private String myaddress;
    private String address;
    private int start;
    private int end;

    public DownloadTread(String address, String myaddress, int start, int end) {
        this.myaddress = myaddress;
        this.address = address;
        this.start = start;
        this.end = end;
    }

    @Override
    public void run() {
        //去服务器的文件的指定位置开始、结束下载
        //放入到空壳文件的指定位置
        HttpURLConnection conn = null;
        RandomAccessFile file = null;
        try {
            URL url = new URL(address);
            conn = (HttpURLConnection) url.openConnection();

            //告诉服务器下载范围
            conn.setRequestProperty("Range","bytes=" + start+"-" + end);

            InputStream in = conn.getInputStream();

            file = new RandomAccessFile(myaddress,"rw");

            //指定需要写入内容的开始位置
            file.seek(start);

            int len = -1;

            byte[] cache = new byte[1024];

            while((len = in.read(cache)) != -1){
                file.write(cache,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(file != null){
                try {
                    file.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(conn != null){
                conn.disconnect();
            }
        }
    }
}